package pl.com.hme.kody.pocztowe.ui;

import java.util.ArrayList;
import java.util.Collections;

import pl.com.hme.kody.pocztowe.R;
import pl.com.hme.kody.pocztowe.adapter.ResultAdapter;
import pl.com.hme.kody.pocztowe.asynctask.PnaAsyncTask;
import pl.com.hme.kody.pocztowe.asynctask.PnaCodeResultCallback;
import pl.com.hme.kody.pocztowe.data.PointOfInterest;
import pl.com.hme.kody.pocztowe.data.SearchResults;
import pl.com.hme.kody.pocztowe.service.CodeQueryService;
import pl.com.hme.kody.pocztowe.service.DetachableResultReceiver;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class ResultsActivity extends Activity implements PnaCodeResultCallback {

	private static final String TAG = "ResultsActivity";
	public DetachableResultReceiver mReceiver = new DetachableResultReceiver(new Handler());
	private ListView mResultListView;
	private TextView mProgressTextView;
	private ViewSwitcher mViewSwitcher;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_results);

		mProgressTextView = (TextView) findViewById(R.id.text_view_results_loading_text);
		mViewSwitcher = (ViewSwitcher) findViewById(R.id.view_switcher_codes_results);
		mResultListView = (ListView) findViewById(R.id.list_codes_results);

		new PnaAsyncTask(getApplicationContext(), this).execute(new String[] {
				getIntent().getStringExtra(CodeQueryService.EXTRA_QUERY_CITY),
				getIntent().getStringExtra(CodeQueryService.EXTRA_QUERY_STREET) });

		// Intent intent = new Intent(Intent.ACTION_RUN, null, this,
		// CodeQueryService.class);
		// intent.putExtra(CodeQueryService.EXTRA_COMMAND_STRING,
		// CodeQueryService.COMMAND_GET_POI_SET);
		// intent.putExtra(CodeQueryService.EXTRA_QUERY_CITY,
		// getIntent().getStringExtra(CodeQueryService.EXTRA_QUERY_CITY));
		// intent.putExtra(CodeQueryService.EXTRA_QUERY_STREET,
		// getIntent().getStringExtra(CodeQueryService.EXTRA_QUERY_STREET));
		// intent.putExtra(CodeQueryService.EXTRA_STATUS_RECEIVER, mReceiver);
		// startService(intent);
	}

	@Override
	public void publishProgress(String progressMessage) {
		mProgressTextView.setText(progressMessage);
	}

	@Override
	public void receiveResults(final SearchResults results) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				ArrayList<PointOfInterest> collection = (ArrayList<PointOfInterest>) results.getPois();

				if (collection.size() > 0) {
					Collections.sort(collection);
					mResultListView.setAdapter(new ResultAdapter(collection, ResultsActivity.this
							.getApplicationContext()));
					mViewSwitcher.showNext();
				} else {
					mProgressTextView.setText(R.string.no_search_results);
				}
			}
		});

	}

	@Override
	public void reportError(Exception e) {
		mProgressTextView.setText(e.getMessage());
		Log.w(TAG,e);

	}


}
